package com.netease.videoLoaderDemo;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.netease.neliveplayer.playerkit.sdk.view.AdvanceTextureView;
import com.netease.neliveplayer.playerkit.videoLoader.OnVideoItemScrollEventHandler;
import com.netease.neliveplayer.playerkit.videoLoader.VideoLoader;
import com.netease.neliveplayer.playerkit.videoLoader.VideoLoaderImpl;
import com.netease.videoLoaderDemo.service.VideoInfo;
import com.netease.videoSlidingKit.R;

import java.util.ArrayList;

public class VideoItemActivity extends AppCompatActivity {
    public String tag = "VideoItemActivity";
    private ViewPager2 viewPage;
    private OnVideoItemScrollEventHandler mOnVideoItemScrollEventHandler;
    private ArrayList<VideoInfo> mVideoInfoList;
    private SparseArray<LiveViewPagerFragment> saFragment;
    private ArrayList<SpriteImageInfo> spriteImageInfoList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_live_detail_activity);
        initView();
        setDarkStatusIcon(true);
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        VideoLoaderImpl.getInstance().setScrollMode(false);
    }

    @Override
    protected void onPause(){
        super.onPause();
        VideoLoaderImpl.getInstance().playPause();
    }
    @Override
    protected void onResume() {
       super.onResume();
        VideoLoaderImpl.getInstance().playResume();
    }
    private void setDarkStatusIcon(boolean b) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x开始需要把颜色设置透明，否则导航栏会呈现系统默认的浅灰色
                View decorView = getWindow().getDecorView();
                //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        //| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //在6.0增加了View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR，
                // 这个字段就是把状态栏标记为浅色，然后状态栏的字体颜色自动转换为深色
                option = option | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                decorView.setSystemUiVisibility(option);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                attributes.flags = attributes.flags | flagTranslucentStatus;
                getWindow().setAttributes(attributes);
            }
        }
    }

    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(params);
        }
        viewPage = findViewById(R.id.viewPager2);
        saFragment = new SparseArray<>();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Intent intent = getIntent();
        int selectedVideoIndex = intent.getIntExtra("VideoSeletedPosition", 0);
        mVideoInfoList = intent.getParcelableArrayListExtra("VideoInfoList");
        spriteImageInfoList  = intent.getParcelableArrayListExtra("SpriteInfoList");
        mOnVideoItemScrollEventHandler = new OnVideoItemScrollEventHandler(viewPage) {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                //
            }
            @Override
            public void onPageStartLoading(int position) {

            }

            @Override
            public void onPageLoaded(int position) {
//
            }

            @Override
            public void onPageLeft(int position) {
                Log.d(tag, "onPageHide, position:$position");
                saFragment.get(position).stopFragment();
            }

            @Override
            public AdvanceTextureView onRequireRenderVideo(int position) {
                View view = saFragment.get(position).getView();
                if(view == null)
                    return null;
                return view.findViewById(R.id.live_surface);
            }

            @Override
            public ProgressBar onRequireSeekBar(int position) {
                View view = saFragment.get(position).getView();
                if(view == null)
                    return null;
                return view.findViewById(R.id.seekBar);
            }
        };
        ArrayList<VideoLoader.VideoInfo> list = new ArrayList<>();
        for(VideoInfo info: mVideoInfoList){
            VideoLoader.VideoInfo it = new VideoLoader.VideoInfo(info.vId, info.url);
            list.add(it);
        }
        mOnVideoItemScrollEventHandler.updateVideoList(list);
        // 设置vp当前页面外的页面数
        viewPage.setOffscreenPageLimit(1);
        FragmentStateAdapter adapter = new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                VideoInfo info = mVideoInfoList.get(position % mVideoInfoList.size());
                SpriteImageInfo spriteImageInfo = spriteImageInfoList.get(position % spriteImageInfoList.size());
                LiveViewPagerFragment fragment = LiveViewPagerFragment.newInstance(info, mOnVideoItemScrollEventHandler, position, spriteImageInfo);
                saFragment.put(position, fragment);
                mOnVideoItemScrollEventHandler.onVideoCreated(position);
                return fragment;
            }

            @Override
            public int getItemCount() {
                return Integer.MAX_VALUE;
            }
        };
        viewPage.setAdapter(adapter);
        viewPage.setUserInputEnabled(true);
        viewPage.registerOnPageChangeCallback(mOnVideoItemScrollEventHandler);
        if(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % mVideoInfoList.size() + selectedVideoIndex == 0){
            mOnVideoItemScrollEventHandler.onPageSelected(0);
        }
        else {
            viewPage.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % mVideoInfoList.size() + selectedVideoIndex,
                    false);
        }
    }
}
